package com.maolong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.maolong.server.exception.DeleteException;
import com.maolong.common.consitant.ExceptionMessage;
import com.maolong.common.consitant.ResultConstant;
import com.maolong.pojo.fuction.result.PageResult;
import com.maolong.util.AliOssUtil;
import com.maolong.mapper.ProductMapper;
import com.maolong.pojo.dto.ProdDTO;
import com.maolong.pojo.dto.ProdSaveDTO;
import com.maolong.pojo.entity.Product;
import com.maolong.pojo.vo.Top6Product;
import com.maolong.pojo.vo.TopInOut;
import com.maolong.service.IProductService;
import org.slf4j.MDC;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author maolong
 * @since 2025-02-09
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private AliOssUtil aliOssUtil;

    /**
     * 分页查询
     * @param prodDTO
     * @return
     */
    @Override
    public PageResult query(ProdDTO prodDTO) {
        Page<Product> page = new Page<>(prodDTO.getPage(), prodDTO.getLimit());
        IPage<Product> productIPage = productMapper.selectByConditions(page,prodDTO);
        return new PageResult(productIPage.getRecords(), Integer.valueOf((int) productIPage.getTotal()));
    }

    /**
     * 删除商品
     * @param ids
     */

    @Override
    public void delete(Integer ids) {
        int i = productMapper.deleteById(ids);
        if(i==0){
            throw new DeleteException(ExceptionMessage.DELETE_ERROR);
        }
    }

    /**
     * 保存或者更新库存
     * @param prodDTO
     */

    @Override
    public void save(ProdSaveDTO prodSaveDTO) throws Exception {
        //判断id是否为空
        Product product = new Product();
        BeanUtils.copyProperties(prodSaveDTO,product);
        product.setEditTime(LocalDateTime.now());

        if(prodSaveDTO.getId()==null){
            product.setAddUser(Long.valueOf(MDC.get(ResultConstant.USER_ID)));
            product.setEditUser(Long.valueOf(MDC.get(ResultConstant.USER_ID)));
            product.setAlarm(0);
            product.setInventory(0);
            product.setInventorySum(0);
            product.setInventoryRemove(0);
            product.setAddTime(LocalDateTime.now());
            productMapper.insert(product);

            product.setImg(aliOssUtil.generateBarcodeAndUpload(String.valueOf(product.getId()),product.getCode()));
            productMapper.updateById(product);
        }else
            productMapper.updateById(product);

    }

    /**
     * 获取库存量的前5名
     * @return
     */
    @Override
    public List<Top6Product> getTop5Product() {
        List<Product> inventory = productMapper.selectList(new QueryWrapper<Product>()
                .orderBy(true, false, "inventory").last("limit 6"));

        List<Top6Product> collect = inventory.stream().map(product -> {
            Top6Product top6Product = new Top6Product();
            top6Product.setName(product.getName());
            top6Product.setValue(product.getInventory());
            return top6Product;
        }).collect(Collectors.toList());
        return collect;
    }

    @Override
    public List<Top6Product> getTop5ProductRemove() {
        List<Product> inventory = productMapper.selectList(new QueryWrapper<Product>()
                .orderBy(true, false, "inventory_remove").last("limit 6"));
        List<Top6Product> collect = inventory.stream().map(product -> {
            Top6Product top6Product = new Top6Product();
            top6Product.setName(product.getName());
            top6Product.setValue(product.getInventoryRemove());
            return top6Product;
        }).collect(Collectors.toList());
        return collect;
    }


    @Override
    public TopInOut getTop5ProductIn() {
        List<Product> inventory = productMapper.selectList(new QueryWrapper<Product>());
        TopInOut topInOut = new TopInOut();
        topInOut.setName(inventory.stream().map(Product::getName).collect(Collectors.toList()));
        topInOut.setValue(inventory.stream().map(Product::getInventorySum).collect(Collectors.toList()));
        return topInOut;
    }

    @Override
    public TopInOut getTop5ProductOut() {
        List<Product> inventory = productMapper.selectList(new QueryWrapper<Product>());
        TopInOut topInOut = new TopInOut();
        topInOut.setName(inventory.stream().map(Product::getName).collect(Collectors.toList()));
        topInOut.setValue(inventory.stream().map(Product::getInventoryRemove).collect(Collectors.toList()));
        return topInOut;
    }


}
